/*********************************************************************************************************************
 * COPYRIGHT NOTICE
 * Copyright (c) 2018,逐飞科技
 * All rights reserved.
 * 技术讨论QQ群：	一群：179029047(已满)
 *					二群：244861897(已满)
 *					三群：824575535
 *
 * 以下所有内容版权均属逐飞科技所有，未经允许不得用于商业用途，
 * 欢迎各位使用并传播本程序，修改内容时必须保留逐飞科技的版权声明。
 *
 * @file			zf_ctimer.h
 * @company			成都逐飞科技有限公司
 * @author			逐飞科技(QQ3184284598)
 * @version			查看doc内version文件 版本说明
 * @Software		IAR 8.3 or MDK 5.24
 * @Taobao			https://seekfree.taobao.com/
 * @date			2020-03-25
 * @note			本文件作为 LPC55S 系列芯片开源库外设文件
					提供 CTIMER 外设驱动
 ********************************************************************************************************************/

#include "zf_ctimer.h"
#include "zf_gpio.h"
#include "zf_isr.h"

uint8_t zf_ctimer_pwmfreq_ch[5];							// 统一的频率
CTIMER_Type *ctimer_list[5] = {CTIMER0, CTIMER1, CTIMER2, CTIMER3, CTIMER4};
IRQn_Type ctimer_irqn[5] = {CTIMER0_IRQn, CTIMER1_IRQn, CTIMER2_IRQn, CTIMER3_IRQn, CTIMER4_IRQn};
clock_attach_id_t  ctimer_clock_list[5] = {kFRO_HF_to_CTIMER0, kFRO_HF_to_CTIMER1, kFRO_HF_to_CTIMER2, kFRO_HF_to_CTIMER3, kFRO_HF_to_CTIMER4};

//-------------------------------------------------------------------------------------------------------------------
//	@brief		获取 CTIMER 定时器的默认设置
//	@param		config			结构体
//	Sample usage:				zf_sct_get_default_config(&config);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_get_default_config (ctimer_config_t *config)
{
	CTIMER_GetDefaultConfig (config);
//	config->mode = kCTIMER_TimerMode;																				// 设置定时器模式
//	config->input = kCTIMER_Capture_0;																				// 这里是输入模式下用到 PWM 模式不使用
//	config->prescale = 0;																							// 预分频 0 也就是不分频
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 默认初始化
//	@param		index			CTimer index
//	@param		pwmfreq_hz		设置的频率
//	@param		cycleChannel	周期通道
//	Sample usage:				zf_ctimer_timer_init(CTM_0, CTM_CH_3, 10000);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_timer_init (ctimer_index_list index, ctimer_channel_index_list cycleChannel, uint32_t pwmfreq_hz)
{
	ctimer_config_t config_ct;
	zf_ctimer_get_default_config(&config_ct);
	CLOCK_AttachClk(ctimer_clock_list[index]);
	CTIMER_Init(ctimer_list[index], &config_ct);

	zf_ctimer_pwmfreq_ch[index] = cycleChannel;
	ctimer_list[index]->MCR |= ((0x00000002)<<((uint32_t)cycleChannel*3));											// 设置周期通道
	ctimer_list[index]->MR[cycleChannel] = CLOCK_GetCTimerClkFreq(0U) / pwmfreq_hz - 1;								// 选定周期通道写入周期计算值
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 输入捕获初始化
//	@param		index			CTimer index
//	@param		mode			输入捕获模式
//	@param		pin				通道管脚
//	Sample usage:				zf_ctimer_capture_init(CTM_1, CTIMER_TIMER_MODE_CAP_RISING_EDGE, CT_INP07_P1_15);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_capture_init (ctimer_index_list index, ctimer_timer_mode_list mode, ctimer_capture_pin_list pin)
{
	volatile uint32_t *proint;
	ctimer_config_t config_ct;

	zf_ctimer_get_default_config(&config_ct);
	CLOCK_AttachClk(ctimer_clock_list[index]);
	config_ct.mode = (ctimer_timer_mode_t)mode;
	config_ct.input = (ctimer_capture_channel_t)CTM_CH_0;
	CTIMER_Init(ctimer_list[index], &config_ct);

	INPUTMUX_Init(INPUTMUX);
	switch((uint8_t)pin)
	{
		case CT_INP00_P0_01:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer4Captsel);break;
			}
			break;
		case CT_INP00_P0_13:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp0ToTimer4Captsel);break;
			}
			break;
		case CT_INP01_P0_02:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer4Captsel);break;
			}
			break;
		case CT_INP01_P0_14:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp1ToTimer4Captsel);break;
			}
			break;
		case CT_INP02_P1_00:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer4Captsel);break;
			}
			break;
		case CT_INP02_P1_28:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp2ToTimer4Captsel);break;
			}
			break;
		case CT_INP03_P1_01:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer4Captsel);break;
			}
			break;
		case CT_INP03_P1_26:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp3ToTimer4Captsel);break;
			}
			break;
		case CT_INP04_P1_09:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer4Captsel);break;
			}
			break;
		case CT_INP04_P0_16:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp4ToTimer4Captsel);break;
			}
			break;
		case CT_INP05_P1_11:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp5ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp5ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp5ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp5ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp5ToTimer4Captsel);break;
			}
			break;
		case CT_INP06_P1_13:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp6ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp6ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp6ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp6ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp6ToTimer4Captsel);break;
			}
			break;
		case CT_INP07_P1_15:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp7ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp7ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp7ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp7ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp7ToTimer4Captsel);break;
			}
			break;
		case CT_INP08_P0_24:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp8ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp8ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp8ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp8ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp8ToTimer4Captsel);break;
			}
			break;
		case CT_INP09_P0_25:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp9ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp9ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp9ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp9ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp9ToTimer4Captsel);break;
			}
			break;
		case CT_INP10_P0_10:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp10ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp10ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp10ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp10ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp10ToTimer4Captsel);break;
			}
			break;
		case CT_INP11_P0_28:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp11ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp11ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp11ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp11ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp11ToTimer4Captsel);break;
			}
			break;
		case CT_INP12_P0_04:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp12ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp12ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp12ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp12ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp12ToTimer4Captsel);break;
			}
			break;
		case CT_INP13_P0_06:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp13ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp13ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp13ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp13ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp13ToTimer4Captsel);break;
			}
			break;
		case CT_INP14_P1_20:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer4Captsel);break;
			}
			break;
		case CT_INP14_P0_26:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp14ToTimer4Captsel);break;
			}
			break;
		case CT_INP15_P0_20:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer4Captsel);break;
			}
			break;
		case CT_INP15_P0_22:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp15ToTimer4Captsel);break;
			}
			break;
		case CT_INP16_P0_15:
			switch((uint8_t)index)
			{
				case CTM_0:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp16ToTimer0Captsel);break;
				case CTM_1:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp16ToTimer1Captsel);break;
				case CTM_2:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp16ToTimer2Captsel);break;
				case CTM_3:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp16ToTimer3Captsel);break;
				case CTM_4:	INPUTMUX_AttachSignal(INPUTMUX, 0, kINPUTMUX_CtimerInp16ToTimer4Captsel);break;
			}
			break;
	}
	INPUTMUX_Deinit(INPUTMUX);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 中断使能
//	@param		index			CTimer index
//	@param		handler			中断处理函数
//	Sample usage:				zf_ctimer_intterupt_enable(CTM_1, handler);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_intterupt_enable (ctimer_index_list index, void handler(void))
{
	ctimer_list[index]->MCR |= ((0x00000001)<<(zf_ctimer_pwmfreq_ch[index]*3));										// 设置周期通道

	ctimer_irqn_func[index] = handler;
	EnableIRQ(ctimer_irqn[index]);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 中断失能
//	@param		index			CTimer index
//	@param		handler			中断处理函数
//	Sample usage:				zf_ctimer_intterupt_enable(CTM_1);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_intterupt_disable (ctimer_index_list index)
{
	DisableIRQ(ctimer_irqn[index]);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 启动
//	@param		index			CTimer index
//	Sample usage:				zf_ctiemr_start(CTM_1);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctiemr_start (ctimer_index_list index)
{
	CTIMER_StartTimer(ctimer_list[index]);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 停止
//	@param		index			CTimer index
//	Sample usage:				zf_ctiemr_stop(CTM_1);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctiemr_stop (ctimer_index_list index)
{
	CTIMER_StopTimer(ctimer_list[index]);
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 复位计数器
//	@param		index			CTimer index
//	Sample usage:				zf_ctimer_reset_count(CTM_1);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_reset_count (ctimer_index_list index)
{
	ctimer_list[index]->TCR |= 0x00000002;
	while(zf_ctimer_get_count(index))
	{
	}
	ctimer_list[index]->TCR &= ~0x00000002;
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER 读取计数器
//	@param		index			CTimer index
//	Sample usage:				zf_ctimer_get_count(CTM_1);
//-------------------------------------------------------------------------------------------------------------------
uint32_t zf_ctimer_get_count (ctimer_index_list index)
{
	return ctimer_list[index]->TC;
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER PWM启动
//	@param		index			CTimer index
//	@param		selectChannel	选择通道
//	Sample usage:				zf_ctimer_pwm_setup(CTM_0, CTM_CH_3);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_pwm_setup (ctimer_index_list index, ctimer_channel_index_list selectChannel)
{
	uint32_t reg;
	assert(selectChannel != zf_ctimer_pwmfreq_ch[index]);

	// 周期通道不能与选定通道是同一个通道
	// 同时启用多个通道时也请注意不要将周期通道用作选定pwm通道
	// 一个定时器最多三个 pwm

	ctimer_list[index]->PWMC |= (1UL << (uint32_t)selectChannel);													// 指定所选通道为 PWM
	reg = ctimer_list[index]->MCR;																					// 先读取 MCR 避免影响其他通道
	reg &=
	~((uint32_t)
		(
			(uint32_t)CTIMER_MCR_MR0R_MASK |																		// MRnI 清空
			(uint32_t)CTIMER_MCR_MR0S_MASK |																		// MRnR 清空
			(uint32_t)CTIMER_MCR_MR0I_MASK																			// MRnS 清空
		) << ((uint32_t)selectChannel * 3U)																			// 位移到对应通道
	);

	ctimer_list[index]->MCR = reg;																					// 再赋值回 MCR
	ctimer_list[index]->MR[selectChannel] = (ctimer_list[index]->MR[zf_ctimer_pwmfreq_ch[index]] * 50) / 100;		// 写入选定通道指定脉宽长度计算值
}

//-------------------------------------------------------------------------------------------------------------------
//	@brief		CTIMER PWM 更新占空比
//	@param		index			CTimer index
//	@param		selectChannel	选择通道
//	@param		dutyCyclePercent占空比
//	Sample usage:				zf_ctimer_pwm_update_duty(CTM_0, CTM_CH_3, 50);
//-------------------------------------------------------------------------------------------------------------------
void zf_ctimer_pwm_update_duty (ctimer_index_list index, ctimer_channel_index_list selectChannel, uint8_t dutyCyclePercent)
{
	uint32_t pulsePeriod = 0, period;
	assert(selectChannel != zf_ctimer_pwmfreq_ch[index]);

	period = ctimer_list[index]->MR[zf_ctimer_pwmfreq_ch[index]];

	if (dutyCyclePercent == 0U)
	{
		pulsePeriod = period + 1U;
	}
	else
	{
		pulsePeriod = (period * (100U - (uint32_t)dutyCyclePercent)) / 100U;
	}

	ctimer_list[index]->MR[selectChannel] = pulsePeriod;
}

